<?php
/*
*This file is contain function to convert datas from temp database to VoteThailandPoll format.
*
*/
function VoteThailandPoll_import_house()
{
  $table = FormUtil::getPassedValue ('table', false , 'GET');
  
  
  $query = "SELECT DISTINCT house_no,moo,village,tambol,district,province,trok,soi,road FROM temp.".$table;
  
  $column = array(  "no",
                    "vil_moo",
                    "vil_id",
                    "tam_id",
                    "dis_id",
                    "pro_id",
                    "trok",
                    "soi",
                    "road"
                    );
  $result = DBUtil::executeSQL($query);
  $objectArray = DBUtil::marshallObjects ($result, $column);
  if(!FormUtil::getPassedValue ('offset', false , 'GET')){
    DBUtil::insertObjectArray($objectArray, 'votethailandpoll_house_no', true);
  }
  
  
  $step=300;
  $startoffset = FormUtil::getPassedValue ('offset', null , 'GET') ? FormUtil::getPassedValue ('offset', null , 'GET') : 0;
  $endoffset = (($startoffset+$step)>count($objectArray)) ? count($objectArray) : ($startoffset+$step);
  
  echo "Table : ".$table."<br>";
  echo "Step :".$step."<br>";
  echo "Start at ".$startoffset."<br>";
  echo "End at ".$endoffset."<hr>";
  echo "Complete : ".$endoffset." (".number_format((($endoffset/count($objectArray)) * 100), 0)."%)<br>";
  echo "Remain : ".(count($objectArray)-$endoffset)."<hr>";
  

  for($i=$startoffset;$i<$endoffset;$i++){
    $value = $objectArray[$i];
    $query = "SELECT
                count(*)
              FROM
                temp.".$table."
              WHERE
                house_no='".$value['no']."'
              AND
                moo='".$value['vil_moo']."'";
    $result = DBUtil::executeSQL($query);
    $objectArray2 = DBUtil::marshallObjects ($result, array('num'));
    echo "[".$i."] House no : ".$value['no']."(".$value['vil_moo'].") - ".$objectArray2[0]['num']."person.<br />\n";
    $query = "UPDATE
                z_votethailandpoll_house_no
              SET
                hou_unit=".$objectArray2[0]['num']."
              WHERE
                hou_no='".$value['no']."'
              AND
                hou_vil_moo='".$value['vil_moo']."'";
   
    $result = DBUtil::executeSQL($query);
    unset($objectArray2);
  }
  if($endoffset>=count($objectArray))
  {
    echo "Complete : ".$endoffset."/".count($objectArray);
    echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"5;URL=http://".$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF']."?module=VoteThailandPoll&type=import&func=person&table=".$table."\">";
    unset($objectArray);
    unset($result);
    exit;
  }else{
    echo "Complete : ".$endoffset."/".count($objectArray);
    echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"5;URL=http://".$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF']."?module=VoteThailandPoll&type=import&func=house&table=".$table."&offset=".$endoffset."\">";
    //echo "<hr><a href='index.php?module=VoteThailandPoll&type=import&func=housetable1&offset=".$endoffset."'>Next</a>";
    unset($objectArray);
    unset($result);
    exit;
  }
}

function VoteThailandPoll_import_person()
{
  $table = FormUtil::getPassedValue ('table', false , 'GET');
  $startoffset = FormUtil::getPassedValue ('offset', false , 'GET');
  
  $query = "SELECT DISTINCT house_no,moo,village,tambol,district,province,trok,soi,road,first_name,last_name,sex FROM temp.".$table;
  
  $column = array(  "no",
                    "vil_moo",
                    "vil_id",
                    "tam_id",
                    "dis_id",
                    "pro_id",
                    "trok",
                    "soi",
                    "road",
                    "firstname",
                    "lastname",
                    "gen_id"
                    );
  $result = DBUtil::executeSQL($query);
  $objectArray = DBUtil::marshallObjects ($result, $column);
  if(!FormUtil::getPassedValue ('offset', false , 'GET')){
    DBUtil::insertObjectArray($objectArray, 'votethailandpoll_person', true);
  }

 
  $step=300;
  $startoffset = FormUtil::getPassedValue ('offset', null , 'GET') ? FormUtil::getPassedValue ('offset', null , 'GET') : 0;
  $endoffset = (($startoffset+$step)>count($objectArray)) ? count($objectArray) : ($startoffset+$step);
  echo "Table : ".$table."<br>";
  echo "Step :".$step."<br>";
  echo "Start at ".$startoffset."<br>";
  echo "End at ".$endoffset."<br>";
  echo "Complete : ".$endoffset." (".number_format((($endoffset/count($objectArray)) * 100), 0)."%)<br>";
  echo "Remain : ".(count($objectArray)-$endoffset)."<hr>";

  for($i=$startoffset;$i<$endoffset;$i++){
    $value = $objectArray[$i];
    $query = "SELECT
                hou_id
              FROM
                z_votethailandpoll_house_no
              WHERE
                hou_no='".$value['no']."'
              AND
                hou_vil_moo='".$value['vil_moo']."'
              AND
                hou_vil_id='".$value['vil_id']."'";
    $result = DBUtil::executeSQL($query);
    $objectHouse = DBUtil::marshallObjects ($result, array('hou_id'));
    
    echo "[".$i."] Person : ".$value['firstname']." ".$value['lastname']." - House ID : ".$objectHouse[0]['hou_id']."<br />\n";
    
    $query = "UPDATE
                z_votethailandpoll_person
              SET
                per_hou_id=".$objectHouse[0]['hou_id']."
              WHERE
                per_firstname='".$value['firstname']."'
              AND
                per_lastname='".$value['lastname']."'";

    
    $result = DBUtil::executeSQL($query);
    unset($objectHouse);
    unset($result);
    unset($value);
  }
  if($endoffset>=count($objectArray))
  {
    echo "Complete : ".$endoffset."/".count($objectArray);
    unset($objectArray);
    unset($result);
    exit;
  }else{

    echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"5;URL=http://".$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF']."?module=VoteThailandPoll&type=import&func=person&table=".$table."&offset=".$endoffset."\">";
    //echo "<hr><a href='index.php?module=VoteThailandPoll&type=import&func=housetable1&offset=".$endoffset."'>Next</a>";
    unset($objectArray);
    unset($result);
    exit;
  }
}

function getHouseId()
{
    foreach($objectArray as $id=>$array){
    if(($array['no']=="108")&&($array['vil_moo']==1))
    $value[] = $id;
  }
}

?>